package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.vo.ForceVersionIosVO;
import com.juyu.miliao.api.domain.vo.ForceVersionVO;
import com.juyu.miliao.api.domain.vo.VersionIosVO;
import com.juyu.miliao.api.service.VersionHistoryService;
import com.juyu.miliao.common.constant.PhoneSystemConstant;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;
import com.juyu.miliao.common.util.CommonUtil;
import com.juyu.miliao.common.util.ServletTool;
import com.juyu.miliao.common.util.VerifyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 强制更新
 *
 * @author jh
 * 2018/9/17
 */
@Api(tags = "版本控制")
@RestController
public class VersionHistoryController {

    @Resource
    private VersionHistoryService versionHistoryService;



    @ApiOperation(value = "比较版本,并获取最新版本", notes = "不需登录,根据forceUpdating判断", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/version/compare-lowest")
    public ForceVersionVO compareLowestVersion(HttpServletRequest request,
                                               @ApiParam(value = "version放head里", required = true) String versionNum,
                                               @ApiParam(value = "phoneSystem放head里", required = true) Integer phoneSystem,
                                               @ApiParam(value = "channel放head里", required = true) String channel,
                                               @ApiParam(value = "appPackageId放head里", required = true) String appPackageId
    ) {
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);
        versionNum = commonHead.getVersionNum();
        phoneSystem = CommonUtil.null2Int(commonHead.getPhoneSystem());
        channel = commonHead.getChannel();
        appPackageId = commonHead.getAppPackageId();

        String[] ids = versionNum.split("\\.");
        if (ids.length < 3) {
            throw new IllegalParamException("版本参数错误");
        }
        String ip = ServletTool.getRemoteHost(request);
        if (phoneSystem == PhoneSystemConstant.ANDROID || phoneSystem == PhoneSystemConstant.IOS) {
            return versionHistoryService.compareVersion(ip,ids, phoneSystem, channel, appPackageId, versionNum);
        }
        throw new IllegalParamException("手机系统类型错误");
    }


    /**
     * 新版ios强更
     * @param request
     * @param versionNum
     * @param phoneSystem
     * @param channel
     * @param appPackageId
     * @return
     */
    @GetMapping(value = "/version/ios")
    public ForceVersionIosVO compareLowestVersionByNewIos(HttpServletRequest request,String versionNum, Integer phoneSystem,
                                                          String channel, String appPackageId
    ) {
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);
        versionNum = commonHead.getVersionNum();
        appPackageId = commonHead.getAppPackageId();
        phoneSystem = CommonUtil.null2Int(commonHead.getPhoneSystem());
        channel = commonHead.getChannel();

        String[] ids = versionNum.split("\\.");
        if (ids.length < 3) {
            throw new IllegalParamException("版本参数错误");
        }
        String ip = ServletTool.getRemoteHost(request);
        if (phoneSystem == PhoneSystemConstant.ANDROID || phoneSystem == PhoneSystemConstant.IOS) {
            ForceVersionVO forceVersionVO =  versionHistoryService.compareVersion(ip,ids, phoneSystem, channel,
                    appPackageId, versionNum);
            ForceVersionIosVO forceVersionIosVO = new ForceVersionIosVO();
            BeanUtils.copyProperties(forceVersionVO,forceVersionIosVO);
            forceVersionIosVO.setAbc(forceVersionVO.getIsAudit());
            return forceVersionIosVO;
        }
        throw new IllegalParamException("手机系统类型错误");
    }


    /**
     * 新版ios强更
     * @param request
     * @param versionNum
     * @param phoneSystem
     * @param channel
     * @param appPackageId
     * @return
     */
    @GetMapping(value = "/type/ios")
    public VersionIosVO versionByNewIos(HttpServletRequest request, String versionNum, Integer phoneSystem,
                                        String channel, String appPackageId
    ) {
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);
        versionNum = commonHead.getVersionNum();
        appPackageId = commonHead.getAppPackageId();
        channel = commonHead.getChannel();
        phoneSystem = CommonUtil.null2Int(commonHead.getPhoneSystem());

        String[] ids = versionNum.split("\\.");
        if (ids.length < 3) {
            throw new IllegalParamException("版本参数错误");
        }
        String ip = ServletTool.getRemoteHost(request);
        if (phoneSystem == PhoneSystemConstant.ANDROID || phoneSystem == PhoneSystemConstant.IOS) {
            ForceVersionVO forceVersionVO =  versionHistoryService.compareVersion(ip,ids, phoneSystem, channel,
                    appPackageId, versionNum);
            VersionIosVO versionIosVO = new VersionIosVO();
            BeanUtils.copyProperties(forceVersionVO,versionIosVO);
            versionIosVO.setJzSC(forceVersionVO.getIsAudit());
            return versionIosVO;
        }
        throw new IllegalParamException("手机系统类型错误");
    }


    /**
     * 获取应用更新信息
     * @param request
     * @return
     */
    @GetMapping(value = "/app/version")
    public Object getAppUpdateInfo(Authentication token, HttpServletRequest request) {
        Subject subject = (Subject) token.getPrincipal();
        long userId = subject.getId();
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);

        String versionNum = commonHead.getVersionNum();

        int phoneSystem = CommonUtil.null2Int(commonHead.getPhoneSystem());

        String appPackageId = commonHead.getAppPackageId();


        return versionHistoryService.getUpdateInfo(userId,phoneSystem,appPackageId,versionNum);
    }

    @GetMapping(value = "/app/ignore/{version}")
    public void getAppIgnoreVersion(Authentication token,@PathVariable(name = "version") String version){
        //// 1.4.2 安卓前端问题 暂时去掉判断，一直返回需要弹窗
//        Subject subject = (Subject) token.getPrincipal();
//        long userId = subject.getId();
//        versionHistoryService.getAppIgnoreVersion(userId,version);
    }
}
